package com.ptswitch.material.service.impl;

import java.math.BigDecimal;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.hxzon.util.DateFormatUtil;
import org.hxzon.util.ExcelUtil;
import org.springframework.stereotype.Service;

import com.ptswitch.material.model.contract.Contract;
import com.ptswitch.material.model.contract.ContractItem;
import com.ptswitch.material.model.contract.ContractPayment;
import com.ptswitch.material.service.ContractExcelService;

@Service
public class ContractExcelServiceImpl extends ExcelUtil implements ContractExcelService {

    @Override
    public void outputContract(String absoluteFilePath, Contract contract) {
        Workbook wb = outputContract(contract);
        write(absoluteFilePath, wb);
    }

    private static void setCellString(Row row, int col, BigDecimal value) {
        setCellString(row, col, value.toString());
    }

    private static void setCellString(Row row, int col, int value) {
        setCellString(row, col, String.valueOf(value));
    }

    @Override
    public Workbook outputContract(Contract contract) {
        Workbook wb = new HSSFWorkbook();
        Sheet sheet = wb.createSheet();
        Row row = null;
        int rowIndex = 0;
        // message
        row = sheet.createRow(rowIndex);
        setCellString(row, 0, "合同编号");
        setCellString(row, 1, "合同状态");
        setCellString(row, 2, "供货商");
        setCellString(row, 3, "合同金额");
        setCellString(row, 4, "已付款");
        setCellString(row, 5, "财务接口人");
        setCellString(row, 6, "生产接口人");
        setCellString(row, 7, "创建人");
        setCellString(row, 8, "创建时间");
        row = sheet.createRow(++rowIndex);
        setCellString(row, 0, contract.getCode());
        setCellString(row, 1, contract.getStatus());
        setCellString(row, 2, contract.getSupplier());
        setCellString(row, 3, contract.getTotalPrice());
        setCellString(row, 4, contract.getAccountPaid());
        setCellString(row, 5, contract.getFinancial().getRealname());
        setCellString(row, 6, contract.getProduction().getRealname());
        setCellString(row, 7, contract.getCreater().getRealname());
        setCellString(row, 8, DateFormatUtil.formatToUniteTime(contract.getCreateTime()));
        // payment
        ++rowIndex;
        row = sheet.createRow(++rowIndex);
        Row row2 = sheet.createRow(++rowIndex);
        setCellString(row, 0, "历次付款金额");
        setCellString(row2, 0, "历次付款时间");
        int colIndex = 1;
        for (ContractPayment payment : contract.getContractPayments()) {
            if (payment.getPaymentType() == ContractPayment.Reply_Payment) {
                setCellString(row, colIndex, payment.isRefund() ? payment.getPayment().negate() : payment.getPayment());
                setCellString(row2, colIndex, DateFormatUtil.formatToUniteTime(payment.getCreateTime()));
                colIndex++;
            }
        }
        // title
        ++rowIndex;
        row = sheet.createRow(++rowIndex);
        setCellString(row, 0, "物料编号");
        setCellString(row, 1, "物料名称");
        setCellString(row, 2, "规格型号");
        setCellString(row, 3, "器件封装");
        setCellString(row, 4, "品牌");
        setCellString(row, 5, "供货商");
        setCellString(row, 6, "购买数量");
        setCellString(row, 7, "送检数量");
        setCellString(row, 8, "入库数量");
        setCellString(row, 9, "单位");
        setCellString(row, 10, "合同编号");
        setCellString(row, 11, "含税单价");
        setCellString(row, 12, "税率");
        setCellString(row, 13, "单价");
        setCellString(row, 14, "单项总价/合同金额");
        // setCellString(row, 10, "历次付款");
        // setCellString(row, 10, "第二次付款");
        // setCellString(row, 10, "第三次付款");
        setCellString(row, 15, "发票开具日期");
        setCellString(row, 16, "合同签订日期");
        setCellString(row, 17, "合同交货日期");
        setCellString(row, 18, "实际交货情况");
        setCellString(row, 19, "验货情况");
        setCellString(row, 20, "验货备注");
        setCellString(row, 21, "完成状态");
        setCellString(row, 22, "备注");
        // rows
        for (ContractItem item : contract.getContractItems()) {
            row = sheet.createRow(++rowIndex);
            contractItemToRow(row, item);
        }
        return wb;
    }

    private static void contractItemToRow(Row row, ContractItem item) {
        Contract contract = item.getContract();
        setCellString(row, 0, item.getCode());
        setCellString(row, 1, item.getName());
        setCellString(row, 2, item.getSpecification());
        setCellString(row, 3, item.getFootprint());
        setCellString(row, 4, item.getBrand());
        setCellString(row, 5, contract.getSupplier());
        setCellString(row, 6, item.getQuantities());
        setCellString(row, 7, item.getQuantities());
        setCellString(row, 8, item.getQuantities());
        setCellString(row, 9, item.getMeasuringUnit());
        setCellString(row, 10, contract.getCode());
        setCellString(row, 11, item.getUnitPriceIncludingTax());
        setCellString(row, 12, item.getTaxRate());
        setCellString(row, 13, item.getUnitPrice());
        setCellString(row, 14, item.getTotalPrice());
        setCellString(row, 15, "");
        setCellString(row, 16, DateFormatUtil.formatToUniteTime(contract.getCreateTime()));
        setCellString(row, 17, item.isReceivingOk() ? item.getReceivingDate() : "");
        setCellString(row, 18, item.getReceivingRemark());
        setCellString(row, 19, item.isExamineOk() ? (item.isPassOk() ? "通过" : "不通过") : "未验");
        setCellString(row, 21, "");
        setCellString(row, 22, item.getRemark());
    }

}
